//
//  AEPersonViewModel.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/12.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEPersonViewModel.h"
#import "AE_Solar-Swift.h"

@implementation AEPersonViewModel

- (void)initialize {
    [super initialize];
    
    self.userInfoModel = [AEAppDelegateTool sharedInstance].userInfoModel;
    
    self.title = [AEStringExten localizedValueWithKey:@"我的"];
    
    self.myBonus = [AEStringExten localizedValueWithKey:@"我的积分"];
    
    self.currentLevel = [AEStringExten localizedValueWithKey:@"当前等级%d级"];
    
    self.shareBonus = [AEStringExten localizedValueWithKey:@"分享领积分"];
    
    self.feedback = [AEStringExten localizedValueWithKey:@"反馈"];
    
}

- (void)userInfoPrase {
    [self.dataList removeAllObjects];
    UIFont *titleFont = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    
//    NSString *title = @"公司名称你哈";
//    float titleWidth = [title widthForFont:titleFont] + 20;
    float valueWidth = 0;
    // 国家
    TYZCommonCellModel *model = [TYZCommonCellModel new];
    model.title = [AEStringExten localizedValueWithKey:@"国家"];
    model.titleWidth = [model.title widthForFont:titleFont] + 2 + 20;
    valueWidth = kScreenW - 20 * 2 - model.titleWidth;
    model.value = self.userInfoModel.country;
    model.valueHeight = [model.value heightForFont:titleFont width:valueWidth];
    model.valueHeight = (model.valueHeight < 22 ? 22 : model.valueHeight);
    model.cellHeight = 14 + model.valueHeight + 14;
    [self.dataList addObject:model];
    
    // 姓名
    model = [TYZCommonCellModel new];
    model.title = [AEStringExten localizedValueWithKey:@"姓名"];
    model.titleWidth = [model.title widthForFont:titleFont] + 2 + 20;
    valueWidth = kScreenW - 20 * 2 - model.titleWidth;
    model.value = self.userInfoModel.name;
    model.valueHeight = [model.value heightForFont:titleFont width:valueWidth];
    model.valueHeight = (model.valueHeight < 22 ? 22 : model.valueHeight);
    model.cellHeight = 14 + model.valueHeight + 14;
    [self.dataList addObject:model];
    
    // 电话
    model = [TYZCommonCellModel new];
    model.title = [AEStringExten localizedValueWithKey:@"电话"];
    model.titleWidth = [model.title widthForFont:titleFont] + 2 + 20;
    valueWidth = kScreenW - 20 * 2 - model.titleWidth;
    model.value = self.userInfoModel.phone;
    model.valueHeight = [model.value heightForFont:titleFont width:valueWidth];
    model.valueHeight = (model.valueHeight < 22 ? 22 : model.valueHeight);
    model.cellHeight = 14 + model.valueHeight + 14;
    [self.dataList addObject:model];
    
    // 职位
    model = [TYZCommonCellModel new];
    model.title = [AEStringExten localizedValueWithKey:@"职位"];
    model.titleWidth = [model.title widthForFont:titleFont] + 2 + 20;
    valueWidth = kScreenW - 20 * 2 - model.titleWidth;
    if ([objectNull(self.userInfoModel.position) isEqualToString:@""]) {
        model.value = @"未填";
    } else {
        model.value = self.userInfoModel.position;
    }
    model.valueHeight = [model.value heightForFont:titleFont width:valueWidth];
    model.valueHeight = (model.valueHeight < 22 ? 22 : model.valueHeight);
    model.cellHeight = 14 + model.valueHeight + 14;
    [self.dataList addObject:model];
    
    // 邮箱
    model = [TYZCommonCellModel new];
    model.title = [AEStringExten localizedValueWithKey:@"邮箱"];
    model.titleWidth = [model.title widthForFont:titleFont] + 2 + 20;
    valueWidth = kScreenW - 20 * 2 - model.titleWidth;
    if ([objectNull(self.userInfoModel.email) isEqualToString:@""]) {
        model.value = @"未填";
    } else {
        model.value = self.userInfoModel.email;
    }
    model.valueHeight = [model.value heightForFont:titleFont width:valueWidth];
    model.valueHeight = (model.valueHeight < 22 ? 22 : model.valueHeight);
    model.cellHeight = 14 + model.valueHeight + 14;
    [self.dataList addObject:model];
    
    // 公司名称
    model = [TYZCommonCellModel new];
    model.title = [AEStringExten localizedValueWithKey:@"公司名称"];
    model.titleWidth = [model.title widthForFont:titleFont] + 2 + 20;
    valueWidth = kScreenW - 20 * 2 - model.titleWidth;
    if ([objectNull(self.userInfoModel.companyName) isEqualToString:@""]) {
        model.value = @"未填";
    } else {
        model.value = self.userInfoModel.companyName;
    }
    model.valueHeight = [model.value heightForFont:titleFont width:valueWidth];
    model.valueHeight = (model.valueHeight < 22 ? 22 : model.valueHeight);
    model.cellHeight = 14 + model.valueHeight + 14;
    [self.dataList addObject:model];
    
    // 公司网站
    model = [TYZCommonCellModel new];
    model.title = [AEStringExten localizedValueWithKey:@"公司网站"];
    model.titleWidth = [model.title widthForFont:titleFont] + 2 + 20;
    valueWidth = kScreenW - 20 * 2 - model.titleWidth;
    if ([objectNull(self.userInfoModel.companyWebsite) isEqualToString:@""]) {
        model.value = @"未填";
    } else {
        model.value = self.userInfoModel.companyWebsite;
    }
    model.valueHeight = [model.value heightForFont:titleFont width:valueWidth];
    model.valueHeight = (model.valueHeight < 22 ? 22 : model.valueHeight);
    model.cellHeight = 14 + model.valueHeight + 14;
    [self.dataList addObject:model];
    
    // 备注
    model = [TYZCommonCellModel new];
    model.title = [AEStringExten localizedValueWithKey:@"备注"];
    model.titleWidth = [model.title widthForFont:titleFont] + 2 + 20;
    valueWidth = kScreenW - 20 * 2 - model.titleWidth;
    if ([objectNull(self.userInfoModel.remarks) isEqualToString:@""]) {
        model.value = @"未填";
    } else {
        model.value = self.userInfoModel.remarks;
    }
    model.valueHeight = [model.value heightForFont:titleFont width:valueWidth];
    model.valueHeight = (model.valueHeight < 22 ? 22 : model.valueHeight);
    model.cellHeight = 14 + model.valueHeight + 14;
    [self.dataList addObject:model];
}

/// 获取用户信息
/// - Parameter completion: completion description
- (void)requestWithUserInfo:(RequestCompletionBlock)completion {
    @weakify(self);
    NSString *userId = [AEUserCommonInfo getUserId];
    AEUserInfoRequest *request = [[AEUserInfoRequest alloc] initWithUserId:userId];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            self.userInfoModel = respond.data;
            [[AEAppDelegateTool sharedInstance] saveUserInfo:self.userInfoModel];
            [self userInfoPrase];
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

/// 退出登录
/// - Parameter completion: completion description
- (void)requestWithLogout:(RequestCompletionBlock)completion {
    @weakify(self);
    NSString *userId = [AEUserCommonInfo getUserId];
    AEUserLogoutRequest *request = [[AEUserLogoutRequest alloc] initWithUserId:userId];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [AEUserCommonInfo logout]; //设置状态
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

/// 得到配置信息
/// - Parameter completion: completion description
- (void)requestWithSysConfig:(RequestCompletionBlock)completion {
    @weakify(self);
    AESysConfigRequest *request = [[AESysConfigRequest alloc] init];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            self.configModel = respond.data;
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

/// 分享成功后调用的接口
/// - Parameter completion: completion description
- (void)requestWithShareSuccess:(RequestCompletionBlock)completion {
    @weakify(self);
    NSString *userId = [AEUserCommonInfo getUserId];
    AEAccountShareRequest *request = [[AEAccountShareRequest alloc] initWithUserId:userId];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

@end
